/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.view.crud.comparatives;

import java.awt.Frame;
import javax.swing.JDialog;
import systole.domain.report.template.Comparative;
import systole.exceptions.ExceptionDAO;
import systole.utils.ImageUtils;
import systole.view.crud.controller.ControllerEntityEdition;
import systole.view.messages.EntityDescriptions;

/**
 *
 * @author jmj
 */
public class ControllerComparativeEdition extends ControllerEntityEdition {

    public ControllerComparativeEdition() {
        super();
        this.entityName = EntityDescriptions.OBSERVATION;
    }

    @Override
    protected void loadEntityOnForm() {
        Comparative comparativeToLoad = (Comparative) this.curretnEntity;
        JDialogComparatives formToLoad = (JDialogComparatives) this.editForm;

        formToLoad.getjTxtName().setText(comparativeToLoad.getName());
        formToLoad.getjTxtObservation().setText(comparativeToLoad.getDescription());
    }

    @Override
    protected void loadEntityFromForm() {
        Comparative comparativeToLoad = (Comparative) this.curretnEntity;
        JDialogComparatives formToLoad = (JDialogComparatives) this.editForm;

        comparativeToLoad.setName(formToLoad.getjTxtName().getText());
        comparativeToLoad.setDescription(formToLoad.getjTxtObservation().getText());
    }

    @Override
    protected String valid() {
        JDialogComparatives formToLoad = (JDialogComparatives) this.editForm;
        Comparative comparative = (Comparative) this.curretnEntity;

        if ((formToLoad.getjTxtName().getText() == null) || (formToLoad.getjTxtName().getText().isEmpty())) {
            return "Debe ingresar el nombre de la observación";
        }

        try {
            if (this.facadeDB.getComparativeBroker().existComparative(formToLoad.getjTxtName().getText(), comparative.getId())) {
                return "Ya existe una observación con el nombre ingresado";
            }
        } catch (ExceptionDAO ex) {
            this.facadeDB.refreshSession();
            return ex.getMessage();
        }

        if ((formToLoad.getjTxtObservation().getText() == null) || (formToLoad.getjTxtObservation().getText().isEmpty())) {
            return "Debe ingresar la observación";
        }

        return null;
    }

    @Override
    protected void save() throws ExceptionDAO {
        if (this.isEditing()) {
            this.facadeDB.getComparativeBroker().update((Comparative) this.curretnEntity);
        } else {
            this.facadeDB.getComparativeBroker().insert((Comparative) this.curretnEntity);
        }
    }

    @Override
    protected void loadIconOnForm() {
        this.editForm.setIconImage(ImageUtils.buildImage("resources/icons/comparative/comparative16.png"));
    }

    @Override
    protected Object createEntity() throws ExceptionDAO {
       return new Comparative();
    }

    @Override
    protected JDialog createEditionForm(Frame parent) {
       return new JDialogComparatives(parent, this);
    }
}
